#include "ofxFMODSystem.h"
ofxFMODSystem* ofxFMODSystem::instance = NULL;

ofxFMODSystem* ofxFMODSystem::getInstance() {
	if (instance == NULL) {
		instance = new ofxFMODSystem();
	}
	return instance;
	// This line only runs once, thus creating the only instance in existence
	//static ofxFMODSystem *instance = new ofxFMODSystem;
	// dereferencing the variable here, saves the caller from having to use
	// the arrow operator, and removes tempation to try and delete the
	// returned instance.
	//return *instance;
}


 ofxFMODSystem::ofxFMODSystem() {

		// @check why release it ??
		//result = FMOD_System_Release(system);
		//ERRCHECK(result);
		printf("\n(Sound Manager) Initialising FMOD Sound System..." );

		// **** Initialise the FMOD system ****
		result = FMOD_System_Create(&system);		// Create the main system object.
		if (result != FMOD_OK) {
			printf(
				"\n(Sound Manager) FMOD error! (%d) %s\n"
				, result, FMOD_ErrorString(result)
			);
			printf("\nSound Has Been Disabled");
		}

		// Use ALSA if we are running on Linux
		// Allow 100 software mixed voices to be audible at once.
		result = FMOD_System_SetSoftwareChannels(system, 100);
		ERRCHECK(result);
		if (result != FMOD_OK){
			printf("\n(Sound Manager) Software Channels could not be set correctly. Error");
		}

		// Initialize FMOD.
		result = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, 0);
		if (result != FMOD_OK)
		{
			printf("\n(Sound Manager) FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
			printf("\nSound Has Been Disabled");
		}

		result = FMOD_System_GetVersion(system, &version);
		ERRCHECK(result);
		if (version < FMOD_VERSION) {
			printf("\n(Sound Manager) Error!  You are using an old version of FMOD %08x." \
					"This program requires %08x\n"
					,version
					,FMOD_VERSION
			);
		}
		printf("Okay\n");
		ERRCHECK(result);

	} // default constructor available only to members or friends of this class



void ofxFMODSystem::ERRCHECK(FMOD_RESULT result) {
	if (result != FMOD_OK) {
		printf(
			"\n\nFMOD Sound Error! (%d) %s"
			,result
			,FMOD_ErrorString(result)
		);
	}
}
